package com.tsd.wxpay.service.impl;

import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import com.tsd.wxpay.dao.WeixinPayNotifyLogMapper;
import com.tsd.wxpay.entity.WeixinPayNotifyLog;
import com.tsd.wxpay.entity.WeixinPayNotifyLogExt;
import com.tsd.wxpay.service.WeixinPayNotifyLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：WeixinPayNotifyLog 服务实现层
 *
 * @author Hillpool
 * @date 2022/07/08 15:48:46
 */
@Service
public class WeixinPayNotifyLogServiceImpl extends BaseServiceImpl implements WeixinPayNotifyLogService {

    @Resource
    private WeixinPayNotifyLogMapper weixinPayNotifyLogMapper;

    @Override
    public WeixinPayNotifyLog getWeixinPayNotifyLogById(Long id) throws Exception {
        return weixinPayNotifyLogMapper.selectByPrimaryKey(id);
    }

    @Override
    public WeixinPayNotifyLogExt queryBySid(String sid) throws Exception {
        return weixinPayNotifyLogMapper.selectBySid(sid);
    }

    @Override
    public void deleteWeixinPayNotifyLogs(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            WeixinPayNotifyLog old = weixinPayNotifyLogMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            weixinPayNotifyLogMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveWeixinPayNotifyLog(WeixinPayNotifyLog record, SysUser opUser) throws Exception {
        super.checkEmpty(record.getTransaction_id(), "缺少参数：transaction_id");
        WeixinPayNotifyLog old = weixinPayNotifyLogMapper.selectByTransactionId(record.getTransaction_id());
        if (old != null) {
            record.setId(old.getId());
        }
        if (HlpUtils.isEmpty(record.getId())) {
            old = weixinPayNotifyLogMapper.selectByOutTradeNo(record.getOut_trade_no());
            if (old != null) {
                record.setId(old.getId());
            }
        }
        if (HlpUtils.isEmpty(record.getId())) {
            weixinPayNotifyLogMapper.insertSelective(record);
        } else {
            weixinPayNotifyLogMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<WeixinPayNotifyLogExt> findWeixinPayNotifyLogs(Map<String, Object> params, PageBean page) throws Exception {
        return weixinPayNotifyLogMapper.selectByPage(params, page);
    }

    @Override
    public List<WeixinPayNotifyLogExt> findWeixinPayNotifyLogs(Map<String, Object> params) throws Exception {
        return weixinPayNotifyLogMapper.selectByParams(params);
    }

    @Override
    public WeixinPayNotifyLogExt queryByOutTradeNo(String outTradeNo) throws Exception {
        return weixinPayNotifyLogMapper.selectByOutTradeNo(outTradeNo);
    }
}